import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.*;

/**
 * @author : lzp
 * @date : 2021/9/7 16:06
 * @describe : TODO
 * @vision 1.0
 */
public class excel {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\lzp\\Desktop\\test.xlsx");

        if (file.exists()) {
            file.delete();
        }
        BufferedOutputStream out = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\lzp\\Desktop\\test.xlsx");
            out = new BufferedOutputStream(fileOutputStream);
            exportExcel(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}
    /**
     * @param out
     * @throws IOException
     */
    private static void exportExcel(BufferedOutputStream out) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        //Sheet sheet1 = workbook.createSheet();
        sheet.setColumnWidth(0, 10*256);
        Row row = sheet.createRow((short) 0);
        Cell cell = row.createCell((short) 0);
        cell.setCellValue("调色板");

        // 创建一个单元格样式
        XSSFCellStyle style = workbook.createCellStyle();
        cell.setCellStyle(style);

        /*****************************使用默认颜色**************************************************/
        // 填充色
//      style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 字体颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
        /**************************************************************************************/

        /*****************************自定义颜色**************************************************/
        XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFColor color1 = new XSSFColor(new java.awt.Color(0, 0, 0));
        // 字体颜色
        XSSFFont font1 = workbook.createFont();
        font1.setColor(color1);
        style.setFont(font1);
        /**************************************************************************************/
        workbook.write(out);
        //XSSFCell cell = sheet_unit.getRow(writerow).createCell(maxcolumnindex);//注意单元格必须创建
        //XSSFCellStyle cellStyle_date = book.createCellStyle();
        //cellStyle_date.cloneStyleFrom(cell.getCellStyle());
        //XSSFFont font = book.createFont();
        //font.setFontName("Arial");
        //font.setFontHeight(10);
        //cellStyle_date.setFont(font);
        //cellStyle_date.setAlignment(HorizontalAlignment.CENTER);
        //cellStyle_date.setBorderBottom(BorderStyle.THIN);
        //cellStyle_date.setBorderTop(BorderStyle.THIN);
        //cellStyle_date.setBorderLeft(BorderStyle.THIN);
        //cellStyle_date.setBorderRight(BorderStyle.THIN);
        //cell.setCellStyle(cellStyle_date);
    }
}

